﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using MedLink2011.Views.Patients.ViewModels;
using MedLink2011.Web.Services;

namespace MedLink2011.Views.Patients.ChildWindows
{
    public partial class InvoicesAddCW : ChildWindow
    {
        
        readonly int loadedPatient_Id = MedLink2011.Views.Patients.ViewModels.PatientViewModel.DashLoad_PK;
        //private int loadedPatient_Id = 1;
        private EntityQuery<MedLink2011.Web.Models.Insurance> _insuranceQuery;
        private MedLink2011Context _context;
        
        public InvoicesAddCW()
        {
            InitializeComponent();
        LoadComboBoxData();
            
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                Patient_IdTextBox.Text = loadedPatient_Id.ToString(); //make sure is not the primary key of the current form, it needs to be a FOREIGN key.
                Insurance_IDTextBox.Text = Insurance_IDComboBox.SelectedIndex.ToString();
            if (!InvoiceViewModel._invoiceContext.IsLoading)
                {
                    this.InvoicesDataForm.CommitEdit();
                    this.DialogResult = true;
                }
            }
            
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        
        private void LoadComboBoxData()
        {

            //Insurance_IDComboBox
             _context = new MedLink2011Context();
            _insuranceQuery = _context.GetInsurancesQuery();
            _insuranceQuery = _insuranceQuery.OrderBy(e => e.Insurance_ID);
            _context.Load(_insuranceQuery, LoadBehavior.RefreshCurrent, Insurance_IDComboBox_Loaded, null);
            Insurance_IDComboBox.ItemsSource = _context.Insurances;
        }


            private void Insurance_IDComboBox_Loaded(LoadOperation<MedLink2011.Web.Models.Insurance> lo)
            {
                this.Insurance_IDComboBox.SelectedIndex = 1;
            }

            private void InvoicesDataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
            {
                this.Close();
            }

    }
}

